package com.hpradip.android.documentaryHub;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

public class Showcase extends ListActivity {

	private static final int AGREEMENT_REQUEST = 1;
	private DocumentaryHub application;
	protected static List<DocumentaryFeed> searchResults;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		application = (DocumentaryHub) getApplication();
		// if(application.getFeeds()==null)
		// application.loadFeeds(this, new Runnable() {
		// @Override
		// public void run() {
		// populateList();
		// }
		// });
		
		DocumentaryHub.tracker.trackPageView("/" + this.getClass().getSimpleName());
		
		showEULA();

		setContentView(R.layout.showcase);
		ListView lv = getListView();
		lv.setTextFilterEnabled(true);

		ImageButton searchBtn = (ImageButton) findViewById(R.id.menu_search);
		searchBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startSearch();
			}
		});

		lv.setOnItemClickListener(new FeedClickListener());
		Log.d(DocumentaryHub.TAG, "Showcase Activity created");
	}

	private void showEULA() {
		SharedPreferences preferences = getSharedPreferences(getResources().getString(R.string.EULA_preferences_key), Activity.MODE_PRIVATE);
		 if (!preferences.getBoolean(getResources().getString(R.string.EULA_accepted_key), false)) {
		 Intent intent = new Intent(this,EULAActivity.class);
		 startActivityForResult(intent, AGREEMENT_REQUEST);
		 }
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_search:
			startSearch();
			break;
		case R.id.menu_categories:
			Intent intent = new Intent(this, BrowseCategories.class);
			startActivity(intent);
			break;
		case R.id.menu_refresh:
			application.loadFeeds(this, new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					populateList();
				}
			});
			break;
		default:
			break;
		}
		return true;
	}

	protected void startSearch() {
		onSearchRequested();
	}

	@Override
	protected void onResume() {
		DocumentaryHub.tracker.trackPageView("/" + this.getClass().getSimpleName());
		super.onResume();
		if (application.getFeeds() == null)
			application.loadFeeds(this, new Runnable() {
				@Override
				public void run() {
					populateList();
				}
			});
		else if (getListAdapter() == null)
			populateList();
	}

	private void populateList() {
		setListAdapter(new FeedsAdapter(this, R.layout.list_item,
				application.getFeeds()));
	}

	private class FeedClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			DocumentaryFeed feed = (DocumentaryFeed) arg0
					.getItemAtPosition(arg2);
			Intent intent = new Intent(Showcase.this, DetailedInfo.class);
			DataProvider.SelectedFeed = feed;
			startActivity(intent);

		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case AGREEMENT_REQUEST:
			if(resultCode==RESULT_CANCELED)
				{
				Builder builder = new AlertDialog.Builder(this);
				builder.setIcon(android.R.drawable.ic_dialog_alert);
				builder.setMessage(getResources().getString(R.string.app_exit_message));
				builder.setTitle(getResources().getString(R.string.app_exit_title));
				builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						android.os.Process.killProcess(android.os.Process.myPid());
					}
				});
				
				builder.setCancelable(false);
				builder.create().show();
				}else
			if(resultCode==RESULT_OK)
			{
				SharedPreferences preferences = getSharedPreferences(getResources().getString(R.string.EULA_preferences_key), Activity.MODE_PRIVATE);
				 preferences.edit().putBoolean(getResources().getString(R.string.EULA_accepted_key), true).commit();
			}
			else
				finish();

		default:
			break;
		}
	}
	
	@Override
	protected void onDestroy() {
		DocumentaryHub.tracker.dispatch();
		super.onDestroy();
	}
}